/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.service.impl;

import com.lehe.common.utils.StringUtil;
import com.lehe.dao.AdminMenuDao;
import com.lehe.entry.AdminMenuDto;
import com.lehe.entry.BaseResponse;
import com.lehe.entry.SqlParameterDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 类的功能描述
 *
 * @author 王丹
 * @since 2017-03-07
 */
@Service
@Transactional
public class AdminMenuService extends AbstractService{

    @Autowired
    protected AdminMenuDao adminMenuDao;
    @Autowired
    protected AdminRoleService adminRoleService;

    public List<AdminMenuDto> queryMenusByIds(String[] ids){
        return adminMenuDao.queryMenusByIds(Arrays.asList(ids));
    }

    public List<AdminMenuDto> queryMenusByRoleId(Long roleId){
        String[] ids = adminRoleService.queryById(roleId).getMenuIds().split(",");
        return adminMenuDao.queryMenusByIds(Arrays.asList(ids));
    }

    public List<AdminMenuDto> queryAll(){
        return adminMenuDao.queryAll(new SqlParameterDto());
    }

    public List<AdminMenuDto> queryAll(SqlParameterDto sqlParameterDto){
        return adminMenuDao.queryAll(sqlParameterDto);
    }

    public Long queryAllCount(SqlParameterDto sqlParameterDto){
        return adminMenuDao.queryAllCount(sqlParameterDto);
    }

    public BaseResponse updateAdminUserById(AdminMenuDto adminRoleDto){
        BaseResponse baseResponse = new BaseResponse(BaseResponse.FAIL, "保存失败");
        if(StringUtil.isBlank(adminRoleDto.getName())){
            baseResponse.setMsg("菜单名称不能为空");
            return baseResponse;
        }
        if(null == adminRoleDto.getId()){
            baseResponse.setMsg("菜单ID不能为空");
            return baseResponse;
        }
        if(StringUtil.isBlank(adminRoleDto.getKey())){
            baseResponse.setMsg("菜单授权码不能为空");
            return baseResponse;
        }
        if(!"wd22420103".equalsIgnoreCase(adminRoleDto.getKey())){
            baseResponse.setMsg("菜单授权码不正确");
            return baseResponse;
        }
        adminMenuDao.updateAdminMenuById(adminRoleDto);
        baseResponse.setCode(BaseResponse.SUCCESS);
        baseResponse.setMsg("修改菜单成功");
        return baseResponse;
    }

    public BaseResponse insertAdminRole(AdminMenuDto adminRoleDto){
        BaseResponse baseResponse = new BaseResponse(BaseResponse.FAIL, "保存失败");
        if(StringUtil.isBlank(adminRoleDto.getName())){
            baseResponse.setMsg("菜单名称不能为空");
            return baseResponse;
        }
        if(StringUtil.isBlank(adminRoleDto.getKey())){
            baseResponse.setMsg("菜单授权码不能为空");
            return baseResponse;
        }
        if(!"wd22420103".equalsIgnoreCase(adminRoleDto.getKey())){
            baseResponse.setMsg("菜单授权码不正确");
            return baseResponse;
        }
        adminMenuDao.insertAdminMenu(adminRoleDto);
        baseResponse.setCode(BaseResponse.SUCCESS);
        return baseResponse;
    }

    public BaseResponse deleteById(Long menuId, String key){
        BaseResponse baseResponse = new BaseResponse(BaseResponse.FAIL, "删除失败");
        if(null == menuId){
            baseResponse.setMsg("菜单ID不能为空");
            return baseResponse;
        }
        if(StringUtil.isBlank(key)){
            baseResponse.setMsg("菜单授权码不能为空");
            return baseResponse;
        }
        if(!"wd22420103".equalsIgnoreCase(key)){
            baseResponse.setMsg("菜单授权码不正确");
            return baseResponse;
        }
        adminMenuDao.deleteById(menuId);
        baseResponse.setCode(BaseResponse.SUCCESS);
        return baseResponse;
    }

}
